package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.controller.BaseController;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmCar;
import com.car.loan.project.cm.service.ICmCarService;
import com.car.loan.project.cm.util.enunms.CarBrandType;
import com.car.loan.project.cm.util.enunms.CarStatus;
import com.car.loan.project.cm.util.select.CarBaseSelect;
import com.car.loan.project.cm.util.vo.CarBrandBaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/21 19:32
 */
@RestController
@RequestMapping("/cm/car")
@Api(value = "车辆品牌管理", tags = "基础数据-车辆品牌管理接口")
public class CmCarController extends BaseController {

    @Autowired
    private ICmCarService carService;

    /**
     * 获取车辆品牌列表
     */
    @PreAuthorize("@ss.hasPermi('cm:car:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取车辆品牌列表", responseContainer = "list", response = CarBrandBaseVo.class)
    public AjaxResult list(@Valid CarBaseSelect carBaseSelect) {
        IPage<CmCar> page = new Page<>(carBaseSelect.getPageNum(),
            carBaseSelect.getPageSize());
        CmCar carQuery = new CmCar();
        carQuery.setType(CarBrandType.BRAND.type);
        QueryWrapper<CmCar> queryWrapper = new QueryWrapper<>(carQuery);
        queryWrapper.select("id,brand,status");
        queryWrapper.orderByDesc("id");
        if (StringUtils.isNotEmpty(carBaseSelect.getBrand())) {
            queryWrapper.like("brand", carBaseSelect.getBrand());
        }
        IPage<CmCar> cmCarIPage = carService.selectPage(page, queryWrapper);
        List<CmCar> records = cmCarIPage.getRecords();
        if (null != records && records.size() > 0) {
            List<CarBrandBaseVo> carBrandBaseVos = new ArrayList<>();
            records.forEach(car -> {
                CarBrandBaseVo carBrandBaseVo = new CarBrandBaseVo();
                carBrandBaseVo.setId(car.getId());
                carBrandBaseVo.setName(car.getBrand());
                carBrandBaseVo.setStatus(car.getStatus());
                carBrandBaseVos.add(carBrandBaseVo);

            });
            IPage<CarBrandBaseVo> carBrandBaseVoIPage = new Page<>();
            BeanUtils.copyBeanProp(carBrandBaseVoIPage, cmCarIPage);
            carBrandBaseVoIPage.setRecords(carBrandBaseVos);
            return AjaxResult.success(carBrandBaseVoIPage);
        }
        return AjaxResult.success(cmCarIPage);
    }


    @PreAuthorize("@ss.hasPermi('cm:car:add')")
    @Log(title = "车辆品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加车辆品牌")
    public AjaxResult add(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name) {
        CmCar car = new CmCar();
        car.setBrand(name);
        car.setType(CarBrandType.BRAND.type);
        car.setStatus(CarStatus.NORMAL.status);
        return toAjax(carService.insertCmCar(car));
    }


    @PreAuthorize("@ss.hasPermi('cm:car:edit')")
    @Log(title = "车辆品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改车辆品牌")
    public AjaxResult edit(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
        @ApiParam(value = "状态 0=正常 5=停用  10=禁止", required = true) @RequestParam("status") Integer status,
        @ApiParam(value = "id", required = true) @RequestParam("id") Long id
    ) {

        CmCar car = carService.selectCmCarById(id);
        car.setStatus(status);
        car.setBrand(name);
        carService.updateCmCar(car);
        return AjaxResult.success();
    }


    @PreAuthorize("@ss.hasPermi('cm:car:list')")
    @GetMapping("/series/list")
    @ApiOperation(value = "获取车辆品牌车系列表", responseContainer = "list", response = CarBrandBaseVo.class)
    public AjaxResult seriesList(@Valid CarBaseSelect carBaseSelect) {
        IPage<CmCar> page = new Page<>(carBaseSelect.getPageNum(), carBaseSelect.getPageSize());
        CmCar carQuery = new CmCar();
        carQuery.setParentId(carBaseSelect.getParentId());
        carQuery.setType(CarBrandType.SERIES.type);
        QueryWrapper<CmCar> queryWrapper = new QueryWrapper<>(carQuery);
        queryWrapper.select("id,series,status");
        queryWrapper.orderByDesc("id");
        if (StringUtils.isNotEmpty(carBaseSelect.getBrand())) {
            queryWrapper.like("series", carBaseSelect.getBrand());
        }
        IPage<CmCar> cmCarIPage = carService.selectPage(page, queryWrapper);
        List<CmCar> records = cmCarIPage.getRecords();
        if (null != records && records.size() > 0) {
            List<CarBrandBaseVo> carBrandBaseVos = new ArrayList<>();
            records.forEach(car -> {
                CarBrandBaseVo carBrandBaseVo = new CarBrandBaseVo();
                carBrandBaseVo.setId(car.getId());
                carBrandBaseVo.setName(car.getSeries());
                carBrandBaseVo.setStatus(car.getStatus());
                carBrandBaseVos.add(carBrandBaseVo);

            });
            IPage<CarBrandBaseVo> carBrandBaseVoIPage = new Page<>();
            BeanUtils.copyBeanProp(carBrandBaseVoIPage, cmCarIPage);
            carBrandBaseVoIPage.setRecords(carBrandBaseVos);
            return AjaxResult.success(carBrandBaseVoIPage);
        }
        return AjaxResult.success(cmCarIPage);
    }


    @PreAuthorize("@ss.hasPermi('cm:car:add')")
    @Log(title = "车辆品牌车系管理", businessType = BusinessType.INSERT)
    @PostMapping("/series")
    @ApiOperation("添加车辆品牌车系")
    public AjaxResult addSeries(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
        @ApiParam(value = "上级", required = true) @RequestParam("parentId") Long parentId) {
        CmCar car = new CmCar();
        car.setSeries(name);
        car.setType(CarBrandType.SERIES.type);
        car.setParentId(parentId);
        car.setStatus(CarStatus.NORMAL.status);
        return toAjax(carService.insertCmCar(car));
    }


    @PreAuthorize("@ss.hasPermi('cm:car:edit')")
    @Log(title = "车辆品牌车系管理", businessType = BusinessType.UPDATE)
    @PutMapping("series")
    @ApiOperation("修改车辆品牌车系")
    public AjaxResult editSeries(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
        @ApiParam(value = "状态 0=正常 5=停用  10=禁止", required = true) @RequestParam("status") Integer status,
        @ApiParam(value = "id", required = true) @RequestParam("id") Long id
    ) {

        CmCar car = carService.selectCmCarById(id);
        car.setStatus(status);
        car.setSeries(name);
        carService.updateCmCar(car);
        return AjaxResult.success();
    }


    @PreAuthorize("@ss.hasPermi('cm:car:list')")
    @GetMapping("/series/model/list")
    @ApiOperation(value = "获取车辆品牌车系车型列表", responseContainer = "list", response = CarBrandBaseVo.class)
    public AjaxResult seriesModelList(@Valid CarBaseSelect carBaseSelect) {
        IPage<CmCar> page = new Page<>(carBaseSelect.getPageNum(), carBaseSelect.getPageSize());
        CmCar carQuery = new CmCar();
        carQuery.setParentId(carBaseSelect.getParentId());
        carQuery.setType(CarBrandType.NAME.type);
        QueryWrapper<CmCar> queryWrapper = new QueryWrapper<>(carQuery);
        queryWrapper.select("id,name,status");
        queryWrapper.orderByDesc("id");
        if (StringUtils.isNotEmpty(carBaseSelect.getBrand())) {
            queryWrapper.like("name", carBaseSelect.getBrand());
        }
        IPage<CmCar> cmCarIPage = carService.selectPage(page, queryWrapper);
        List<CmCar> records = cmCarIPage.getRecords();
        if (null != records && records.size() > 0) {
            List<CarBrandBaseVo> carBrandBaseVos = new ArrayList<>();
            records.forEach(car -> {
                CarBrandBaseVo carBrandBaseVo = new CarBrandBaseVo();
                carBrandBaseVo.setId(car.getId());
                carBrandBaseVo.setName(car.getName());
                carBrandBaseVo.setStatus(car.getStatus());
                carBrandBaseVos.add(carBrandBaseVo);

            });
            IPage<CarBrandBaseVo> carBrandBaseVoIPage = new Page<>();
            BeanUtils.copyBeanProp(carBrandBaseVoIPage, cmCarIPage);
            carBrandBaseVoIPage.setRecords(carBrandBaseVos);
            return AjaxResult.success(carBrandBaseVoIPage);
        }
        return AjaxResult.success(cmCarIPage);
    }

    @PreAuthorize("@ss.hasPermi('cm:car:add')")
    @Log(title = "车辆品牌车系车型管理", businessType = BusinessType.INSERT)
    @PostMapping("/model")
    @ApiOperation("添加车辆品牌车系车型")
    public AjaxResult addModel(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
        @ApiParam(value = "上级", required = true) @RequestParam("parentId") Long parentId) {
        CmCar car = new CmCar();
        car.setName(name);
        car.setParentId(parentId);
        car.setType(CarBrandType.NAME.type);
        car.setStatus(CarStatus.NORMAL.status);
        return toAjax(carService.insertCmCar(car));
    }


    @PreAuthorize("@ss.hasPermi('cm:car:edit')")
    @Log(title = "车辆品牌车系车型管理", businessType = BusinessType.UPDATE)
    @PutMapping("model")
    @ApiOperation("修改车辆品牌车系车型")
    public AjaxResult editModel(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
        @ApiParam(value = "状态 0=正常 5=停用  10=禁止", required = true) @RequestParam("status") Integer status,
        @ApiParam(value = "id", required = true) @RequestParam("id") Long id
    ) {

        CmCar car = carService.selectCmCarById(id);
        car.setStatus(status);
        car.setName(name);
        carService.updateCmCar(car);
        return AjaxResult.success();
    }

}
